<?php # Script 9.4 - view_users02.php
// This script retrieves all the records from the users table.
// This new version links to edit and delete pages.

$page_title = '사용자 리스트';
include('includes/header.html');
?>
	<div id="wrap">
		<div class="navbar navbar-inverse">
			<div class="navbar-inner">
				<div class="container">
					<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</a>
					<a class="brand" href="index.php">User Manager</a>
					<div class="nav-collapse collapse">
						<ul class="nav">
							<li class=""><a href="index.php">홈</a></li>
							<li class=""><a href="register.php">사용자 등록</a></li>
							<li class="active"><a href="view_users02.php">사용자 목록</a></li>
							<li class=""><a href="password.php">비밀번호 변경</a></li>
							<li class=""><a href="#">link five</a></li>
						</ul>
					</div>
				</div>
			</div>
		</div>
		<div class="container">
			<div class="page-header">
    	<!-- Start of the page-specific content. -->
<?php

// Page header:
echo '<h1>사용자 리스트</h1>';

require_once('./mysqli_connect.php'); // Connect to the db.

// Number of records to show per page:
$display = 10;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.

	$pages = $_GET['p'];

} else { // Need to determine.

	// Count the number of records:
	$q = "select count(user_id) from users";
	$r = @mysqli_query($dbc, $q);
	$row = @mysqli_fetch_array($r, MYSQLI_NUM);
	$records = $row[0];

	// Calculate the number of pages...
	if($records > $display) { // More than 1 page.
		$pages = ceil($records/$display);
	} else {
		$pages = 1;
	}

} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
	$start = $_GET['s'];
} else {
	$start = 0;
}

// Make the query:
$q = "select last_name, first_name, date_format(registration_date, '%Y-%m-%d') as dr, user_id from users order by registration_date asc limit $start, $display";
$r = @mysqli_query($dbc, $q); // Run the query.


// Table header:
echo '<table class="table table-striped table-bordered table-hover table-condensed">
		<thead>
            <tr>
            	<th>Edit</th>
            	<th>Delete</th>
                <th>Last Name</th>
                <th>First Name</th>
                <th>Date Registered</th>
            </tr>
        </thead>
        <tbody>';

// Fetch and print all the records:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
	echo '<tr>
			<td><a href="edit_user.php?id='.$row['user_id'].'">Edit</a></td>
			<td><a href="delete_user.php?id='.$row['user_id'].'">Delete</a></td>
            <td>'.$row['last_name'].'</td>
            <td>'.$row['first_name'].'</td>
            <td>'.$row['dr'].'</td>
        </tr>';
} // End of WHILE loop.
    
echo '</tbody>
	</table>'; // Close the table.

mysqli_free_result($r); // Free up the resources.
mysqli_close($dbc); // Close the database connection.

// Make the links to other pages, if necessary.
if ($pages > 1) {
	
	// Add some spacing and start a paragraph:
	echo '<br/><p>';

	// Determine what page the script is on:
	$current_page = ($start/$display) + 1;

	// If it's not the first page, make a Previous button:
	if ($current_page != 1) {
		echo '<button class="btn btn-mini" type="button"><a href="view_users02.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a></button> ';
	}

	// Make all the numbered pages:
	for ($i=1; $i < $pages; $i++) { 
		if ($i != $current_page) {
			echo '<a href="view_users02.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
		} else {
			echo $i . ' ';
		}
	}  // End of FOR loop.

	// If it's not the last page, make a Next button:
	if ($current_page != $pages) {
		echo '<button class="btn btn-mini" type="button"><a href="view_users02.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a></button>';
	}

	echo '</p>'; // Close the paragraph.

} // End of links section.

?>
<?php include('includes/footer.html'); ?>